﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace MvvmTreeView
{
    public partial class TreeNodeItemContent : UserControl
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TreeNodeItemContent), new PropertyMetadata(TextPropertyChanged));
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("ImageProperty", typeof(BitmapImage), typeof(TreeNodeItemContent), new PropertyMetadata(ImagePropertyChanged));
        public static readonly DependencyProperty GlyphProperty = DependencyProperty.Register("GlyphProperty", typeof(BitmapImage), typeof(TreeNodeItemContent), new PropertyMetadata(GlyphPropertyChanged));
        public static readonly DependencyProperty LayoverImageProperty = DependencyProperty.Register("LayoverImageProperty", typeof(BitmapImage), typeof(TreeNodeItemContent), new PropertyMetadata(LayoverImagePropertyChanged));
        public delegate void NodeCreatedHandler(object sender, TreeNodeItemRoutedEventArgs e);
        public event NodeCreatedHandler NodeContentCreated;
        private bool editMode;

        public ITreeView TreeView { get; set; }
        public ITreeNode TreeNode { get; set; }
        public TreeViewItem TreeViewItem { get; set; }
        public Button ButtonContainer { get; set; }

        public TreeNodeItemContent()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ITreeNode treeNode = this.Tag as ITreeNode;
            ITreeView treeView = treeNode.TreeView;

            var parent = VisualTreeHelper.GetParent(this) as FrameworkElement;

            while (!(parent is TreeViewItem))
            {
                parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;

                if (parent is Button)
                {
                    this.ButtonContainer = parent as Button;
                }
            }

            this.TreeViewItem = parent as TreeViewItem;

            this.TreeNode = treeNode;
            this.TreeView = TreeView;

            treeNode.ImageAvailable += new ImageAvailableHandler(ImageAvailable);
            treeNode.GlyphAvailable += new ImageAvailableHandler(GlyphAvailable);
            treeNode.LayoverImageAvailable += new ImageAvailableHandler(LayoverImageAvailable);
            treeNode.StartLoadImage();

            if (NodeContentCreated != null)
            {
                NodeContentCreated(this, new TreeNodeItemRoutedEventArgs(this, treeNode));
            }
        }

        private void GlyphAvailable(ITreeNode node)
        {
            this.Glyph = (BitmapImage)node.Glyph; 
        }

        private void ImageAvailable(ITreeNode node)
        {
            this.Image = (BitmapImage) node.Image;
        }

        private void LayoverImageAvailable(ITreeNode node)
        {
            this.LayoverImage = (BitmapImage)node.LayoverImage;
        }

        private static void TextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TreeNodeItemContent)d).SetText((string) e.NewValue);
        }

        private static void ImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TreeNodeItemContent)d).SetImage((BitmapImage)e.NewValue);
        }

        private static void LayoverImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TreeNodeItemContent)d).SetLayoverImage((BitmapImage)e.NewValue);
        }

        private static void GlyphPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TreeNodeItemContent)d).SetGlyph((BitmapImage)e.NewValue);
        }

        private void SetImage(BitmapImage image)
        {
            NodeImage.Source = image;
        }

        private void SetLayoverImage(BitmapImage image)
        {
            NodeLayoverImage.Source = image;
        }

        private void SetGlyph(BitmapImage image)
        {
            NodeGlyph.Source = image;
        }

        public bool EditMode
        {
            get
            {
                return editMode;
            }

            set
            {
                var wasEditMode = editMode;

                editMode = value;

                if (editMode)
                {
                    TreeNode.FireBeforeLabelEdit();

                    NodeTextBox.Text = NodeTextBlock.Text;

                    NodeTextBlock.Visibility = System.Windows.Visibility.Collapsed;
                    NodeTextBox.Visibility = System.Windows.Visibility.Visible;

                    NodeTextBox.Focus();
                    NodeTextBox.SelectAll();
                }
                else
                {
                    NodeTextBlock.Visibility = System.Windows.Visibility.Visible;
                    NodeTextBox.Visibility = System.Windows.Visibility.Collapsed;

                    this.Text = NodeTextBox.Text;

                    this.TreeViewItem.Focus();

                    if (wasEditMode)
                    {
                        TreeNode.FireAfterLabelEdit(this.Text);
                    }
                }
            }
        }

        private void SetText(string text)
        {
            NodeTextBlock.Text = text;
        }

        public BitmapImage Image
        {
            get
            {
                return (BitmapImage)GetValue(ImageProperty);
            }

            set
            {
                SetValue(ImageProperty, value);
            }
        }

        public BitmapImage LayoverImage
        {
            get
            {
                return (BitmapImage)GetValue(LayoverImageProperty);
            }

            set
            {
                SetValue(LayoverImageProperty, value);
            }
        }

        public BitmapImage Glyph
        {
            get
            {
                return (BitmapImage)GetValue(GlyphProperty);
            }

            set
            {
                SetValue(GlyphProperty, value);
            }
        }

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }

            set
            {
                SetValue(TextProperty, value);
            }
        }

        private void NodeTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.EditMode = false;
        }

        private void NodeTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.EditMode = false;
            }
            else if (e.Key == Key.Escape)
            {
                NodeTextBox.Text = NodeTextBlock.Text;

                this.EditMode = false;
            }
        }
    }
}
